package com.stylefeng.guns.modular.vpn.utils;

import com.stylefeng.guns.modular.vpn.entity.IPInfo;
import io.jsonwebtoken.lang.Strings;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.lionsoul.ip2region.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Objects;

/**
 * @Description TODO
 * @Author Mr.GodRui
 * @Date 2020-04-24 17:00
 **/
@Slf4j
public class Ip2Region {
    private static DbConfig config = null;
    private static DbSearcher searcher = null;

    static {

    }

    public Ip2Region(String dbPath) {
        try {
//            String dbfile = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX+"ip2region.db").getPath();
//            System.out.println(dbfile);
//            log.info("dbFile: {}",dbfile);
            config = new DbConfig();
            searcher = new DbSearcher(config, dbPath);
        }catch (DbMakerConfigException e){
            log.warn("ip2region config init exception:" + e.getMessage());
        }catch (FileNotFoundException e){
            log.warn("ip2region file not found" + e.getMessage());
        }
    }

    public DataBlock parseIp(String ip){
        boolean isIpAddress = Util.isIpAddress(ip);
        if(isIpAddress){
            try {
                DataBlock block = searcher.btreeSearch(ip);
                return block;
            }catch (IOException e){
                log.warn("ip2region parse error" + e.getMessage());
            }
        }
        return null;
    }

    public IPInfo getIPInfo(String ipAddr) {
        try {
            String addr = parseIp(ipAddr).getRegion();
            System.out.println("IP地址： "+addr);
            if(StringUtils.isNotEmpty(addr)){
                String[] array = addr.split("\\|");
                return IPInfo.builder()
                        .country(array[0])
                        .province(array[2])
                        .city(array[3])
                        .operator(array[4])
                        .build();
            }
        } catch (Exception e) {
            log.error("解析IP地址错误  {}",e.getMessage());
        }
        return IPInfo.builder()
                .country("0")
                .province("0")
                .city("0")
                .operator("0")
                .build();
    }

    public static String  getFinalCityName(IPInfo ipInfo){
        if (ipInfo.getCountry().contains("缅甸")){
            return "泰国";
        }
//        if (!ipInfo.getCountry().equals("中国") && !ipInfo.getCountry().equals("0")){
//            return ipInfo.getCountry();
//        }

//        if (StringUtils.isNotEmpty(ipInfo.getCity())
//                && !ipInfo.getCity().equals("0")){
//            return ipInfo.getCity();
//        }

       else if (StringUtils.isNotEmpty(ipInfo.getProvince())
                && !ipInfo.getProvince().equals("0")){
            return (ipInfo.getProvince().contains("省")
                    ?ipInfo.getProvince().replace("省","")
                    :ipInfo.getProvince());
        }
        else if (StringUtils.isNotEmpty(ipInfo.getCountry())
                && !ipInfo.getCountry().equals("0")){
            return ipInfo.getCountry();
        }
        else{
            return "未知IP";
        }

    }

//    public static void main(String args[]) throws Exception{
//        // 格式：国家|大区|省份|城市|运营商
//        System.out.println(getIPInfo("223.146.36.213").toString());
////        System.out.println(Ip2Region.parseIp("240e:379:a2b6:d300:15a1:9041:f5f6:551d").getRegion());
//    }
}
